home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sa85d.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  3.9 KB  |  165 lines

  1. /* Copyright (C) 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sa85d.c,v 1.2.2.1 2000/11/09 22:57:10 rayjj Exp $ */
  20. /* ASCII85Decode filter */
  21. #include "std.h"
  22. #include "strimpl.h"
  23. #include "sa85d.h"
  24. #include "scanchar.h"
  25.  
  26. /* ------ ASCII85Decode ------ */
  27.  
  28. private_st_A85D_state();
  29.  
  30. /* Initialize the state */
  31. private int
  32. s_A85D_init(stream_state * st)
  33. {
  34.     stream_A85D_state *const ss = (stream_A85D_state *) st;
  35.  
  36.     return s_A85D_init_inline(ss);
  37. }
  38.  
  39. /* Process a buffer */
  40. private int a85d_finish(P3(int, ulong, stream_cursor_write *));
  41. private int
  42. s_A85D_process(stream_state * st, stream_cursor_read * pr,
  43.            stream_cursor_write * pw, bool last)
  44. {
  45.     stream_A85D_state *const ss = (stream_A85D_state *) st;
  46.     register const byte *p = pr->ptr;
  47.     register byte *q = pw->ptr;
  48.     const byte *rlimit = pr->limit;
  49.     byte *wlimit = pw->limit;
  50.     int ccount = ss->odd;
  51.     ulong word = ss->word;
  52.     int status = 0;
  53.  
  54.     while (p < rlimit) {
  55.     int ch = *++p;
  56.     uint ccode = ch - '!';
  57.  
  58.     if (ccode < 85) {    /* catches ch < '!' as well */
  59.         if (ccount == 4) {
  60.         /*
  61.          * We've completed a 32-bit group.  Make sure we have
  62.          * room for it in the output.
  63.          */
  64.         if (wlimit - q < 4) {
  65.             p--;
  66.             status = 1;
  67.             break;
  68.         }
  69.         word = word * 85 + ccode;
  70.         q[1] = (byte) (word >> 24);
  71.         q[2] = (byte) (word >> 16);
  72.         q[3] = (byte) ((uint) word >> 8);
  73.         q[4] = (byte) word;
  74.         q += 4;
  75.         word = 0;
  76.         ccount = 0;
  77.         } else {
  78.         word = word * 85 + ccode;
  79.         ++ccount;
  80.         }
  81.     } else if (ch == 'z' && ccount == 0) {
  82.         if (wlimit - q < 4) {
  83.         p--;
  84.         status = 1;
  85.         break;
  86.         }
  87.         q[1] = q[2] = q[3] = q[4] = 0,
  88.         q += 4;
  89.     } else if (scan_char_decoder[ch] == ctype_space)
  90.         DO_NOTHING;
  91.     else if (ch == '~') {
  92.         /* Handle odd bytes. */
  93.         if (p == rlimit) {
  94.         if (last)
  95.             status = ERRC;
  96.         else
  97.             p--;
  98.         break;
  99.         }
  100.         if ((int)(wlimit - q) < ccount - 1) {
  101.         status = 1;
  102.         p--;
  103.         break;
  104.         }
  105.         if (*++p != '>') {
  106.         status = ERRC;
  107.         break;
  108.         }
  109.         pw->ptr = q;
  110.         status = a85d_finish(ccount, word, pw);
  111.         q = pw->ptr;
  112.         break;
  113.     } else {        /* syntax error or exception */
  114.         status = ERRC;
  115.         break;
  116.     }
  117.     }
  118.     pw->ptr = q;
  119.     if (status == 0 && last) {
  120.     if ((int)(wlimit - q) < ccount - 1)
  121.         status = 1;
  122.     else
  123.         status = a85d_finish(ccount, word, pw);
  124.     }
  125.     pr->ptr = p;
  126.     ss->odd = ccount;
  127.     ss->word = word;
  128.     return status;
  129. }
  130. /* Handle the end of input data. */
  131. private int
  132. a85d_finish(int ccount, ulong word, stream_cursor_write * pw)
  133. {
  134.     /* Assume there is enough room in the output buffer! */
  135.     byte *q = pw->ptr;
  136.     int status = EOFC;
  137.  
  138.     switch (ccount) {
  139.     case 0:
  140.         break;
  141.     case 1:        /* syntax error */
  142.         status = ERRC;
  143.         break;
  144.     case 2:        /* 1 odd byte */
  145.         word = word * (85L * 85 * 85) + 0xffffffL;
  146.         goto o1;
  147.     case 3:        /* 2 odd bytes */
  148.         word = word * (85L * 85) + 0xffffL;
  149.         goto o2;
  150.     case 4:        /* 3 odd bytes */
  151.         word = word * 85 + 0xffL;
  152.         q[3] = (byte) (word >> 8);
  153. o2:        q[2] = (byte) (word >> 16);
  154. o1:        q[1] = (byte) (word >> 24);
  155.         q += ccount - 1;
  156.         pw->ptr = q;
  157.     }
  158.     return status;
  159. }
  160.  
  161. /* Stream template */
  162. const stream_template s_A85D_template = {
  163.     &st_A85D_state, s_A85D_init, s_A85D_process, 2, 4
  164. };
  165.